package com.app.controller;


import cn.hutool.core.util.NumberUtil;
import com.app.common.Result;
import com.app.mapper.BusinessMapper;
import com.app.model.BusinessModel;
import com.app.model.SecuritiesTradingZone;
import com.app.model.UserModel;
import com.app.service.ISecuritiesTradingZoneService;
import com.app.service.ITbSellingCustomerService;
import com.app.service.UserService;
import com.app.utils.TokenUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 售劵专区表 前端控制器
 * </p>
 *
 * @author yoominic
 * @since 2024-09-03
 */
@RestController
@RequestMapping("/securitiesTradingZone")
public class SecuritiesTradingZoneController {

    @Autowired
    private ISecuritiesTradingZoneService securitiesTradingZoneService;
    @Autowired
    private UserService userService;


    /**
     * 小程序我要变现
     */
    @PostMapping("/iWantToCashIn")
    public Result iWantToCashIn(HttpServletRequest request,@RequestBody SecuritiesTradingZone securitiesTradingZone) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        UserModel userModel = userService.getUserByOpenid(openId);
        if (userModel == null) {
            return Result.error(405, "用戶不存在");
        }
        return securitiesTradingZoneService.addSecuritiesTradingZone(userModel,securitiesTradingZone);
    }

    @Autowired
    private BusinessMapper businessMapper;

    /**
     * 查询商户变量现值
     *
     * @param request 请求
     * @return {@link Result}
     */
    @GetMapping("/queryMerchantVariablePresentValue")
    public Result queryMerchantVariablePresentValue(HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        UserModel userModel = userService.getUserByOpenid(openId);
        if (userModel == null) {
            return Result.error(405, "用戶不存在");
        }
        BusinessModel businessModel=this.businessMapper.selectAllByUserPhone(userModel.getPhone());
        if(businessModel.getVariablePresent()==null)
        {
            businessModel.setVariablePresent(2000L);
        }
        return Result.success(findMultiples(businessModel.getVariablePresent().intValue()));
    }

    public static void main(String[] args) {
        System.out.println(findMultiples(3000));
    }



    public static List<Integer> findMultiples(int number) {
        List<Integer> multiples = new ArrayList<>();
        int multiple = number;

        // 找出小于10000的number的倍数
        while (multiple < 10000) {
            multiples.add(multiple);
            multiple += number;
        }

        return multiples;
    }

    /**
     * 非商家用户的列表查询
     */
    @GetMapping("/getListWithoutUserId")
    public Result getListWithoutUserId(HttpServletRequest request,@RequestParam(required = false,defaultValue = "1") Integer page,@RequestParam(required = false,defaultValue = "10")Integer limit) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        UserModel userModel = userService.getUserByOpenid(openId);
        if (userModel == null) {
            return Result.error(405, "用戶不存在");
        }
        return securitiesTradingZoneService.getListWithoutUserId(page,limit);
    }


    /**
     * 根据id查看详情（查看收款码的时候用）
     */
    @GetMapping("/getById")
    public Result getPayById(Integer id, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        UserModel userModel = userService.getUserByOpenid(openId);
        if (userModel == null) {
            return Result.error(405, "用戶不存在");
        }
        return securitiesTradingZoneService.getPayById(id);
    }

    /**
     * 商家用户查看自己发布券的列表
     */
    @GetMapping("/getCurrentUserList")
    public Result getCurrentUserList(HttpServletRequest request,@RequestParam(required = false,defaultValue = "1") Integer page,@RequestParam(required = false,defaultValue = "10")Integer limit) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        UserModel userModel = userService.getUserByOpenid(openId);
        if (userModel == null) {
            return Result.error(405, "用戶不存在");
        }
        return securitiesTradingZoneService.getCurrentUserList(userModel.getId(),page,limit);
    }

    @Autowired
    private ITbSellingCustomerService tbSellingCustomerService;

    @GetMapping("/details")
    public Result details(HttpServletRequest request,
                          Integer id) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        UserModel userModel = userService.getUserByOpenid(openId);
        if (userModel == null) {
            return Result.error(405, "用戶不存在");
        }
        SecuritiesTradingZone securitiesTradingZone=this.securitiesTradingZoneService.details(id);
        securitiesTradingZone.setTbSellingCustomer(tbSellingCustomerService.getById(1));
        return Result.success(securitiesTradingZone);
    }




    /**
     * 商家修改自己发布的劵的状态(售罄或者删除)
     */
    @GetMapping("/resetStatus")
    public Result resetStatus(HttpServletRequest request,Integer id,Integer status) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        UserModel userModel = userService.getUserByOpenid(openId);
        if (userModel == null) {
            return Result.error(405, "用戶不存在");
        }
        if (id==null||status==null||id<1||status>3||status<0) {
            return Result.error(400,"参数错误");
        }
        return securitiesTradingZoneService.resetStatus(id,status);
    }



    /**
     * 管理员列表
     *
     * @param page           第页
     * @param limit          限制
     * @param businessName 搜索
     * @return {@link Result}
     */
    @GetMapping("admin/list")
    public Result adminList(@RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "10") Integer limit,
                            @RequestParam(required = false) Integer status,
                            @RequestParam(required = false) String businessName) {
        IPage<SecuritiesTradingZone>  securitiesTradingZoneIPage= securitiesTradingZoneService.adminList(page, limit,status,businessName);
        return Result.success(securitiesTradingZoneIPage);
    }


    /**
     * 管理员详细信息
     * @param id id
     * @return {@link Result}
     */
    @GetMapping("admin/details")
    public Result adminDetails(@RequestParam(value = "id") Integer id) {

        return Result.success(this.securitiesTradingZoneService.details(id));
    }

    /**
     * admin悬挂销售
     * 管理员列表
     *
     * @param status 状态 2删除 0 售罄
     * @param id     id
     * @return {@link Result}
     */
    @GetMapping("admin/update")
    public Result adminHangingSale(@RequestParam(value = "id") Integer id,
                            @RequestParam(value = "status") Integer status) {
        SecuritiesTradingZone securitiesTradingZoneIPage=this.securitiesTradingZoneService.getById(id);
        securitiesTradingZoneIPage.setStatus(status);
        if (status == 0)
        {
            securitiesTradingZoneIPage.setSaleTime(new Date());
        }
        return Result.success(this.securitiesTradingZoneService.updateById(securitiesTradingZoneIPage));
    }
    @GetMapping("update")
    public Result adminHangingSale(@RequestParam(value = "id") Integer id,
                                   @RequestParam(value = "status") Integer status,
                                   HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        UserModel userModel = userService.getUserByOpenid(openId);
        if (userModel == null) {
            return Result.error(405, "用戶不存在");
        }
        SecuritiesTradingZone securitiesTradingZoneIPage = this.securitiesTradingZoneService.getById(id);
        securitiesTradingZoneIPage.setStatus(status);
        if (status == 0)
        {
            securitiesTradingZoneIPage.setSaleTime(new Date());
        }
        return Result.success(this.securitiesTradingZoneService.updateById(securitiesTradingZoneIPage));
    }


    /**
     * 获取剩余金额
     *
     * @param request 请求
     * @return {@link Result}
     */
    @GetMapping("/getRemainingAmount")
    public Result getRemainingAmount(HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        UserModel userModel = userService.getUserByOpenid(openId);
        if (userModel == null) {
            return Result.error(405, "用戶不存在");
        }
        return Result.success(userModel.getVoucher());
    }


}

